package com.lris.handle;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.lris.enumerates.MessageType;
import com.lris.pojodata.Message;
import com.lris.rest.Result;
import com.lris.server.ImServer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

/**
 * 私聊
 */
@Slf4j
public class ChatHandle {
public static  void execute(ChannelHandlerContext ctx, TextWebSocketFrame frame){

    try {
        Message chatMessage = JSON.parseObject(frame.text(), Message.class);
        log.info("消息类型"+chatMessage.getType());

        switch (MessageType.math(chatMessage.getType())){
            //私聊
            //Type 填1   私聊
            case PRIVATE -> {
                if (null==chatMessage.getToId()){
                    ctx.channel().writeAndFlush(Result.fail("发送消息失败,发送消息前,请指定 目标 id"));
                    return;
                }
                Channel channel = ImServer.USERS.get(chatMessage.getToId().toString());
                //如果用户在线或者不活跃
                if (null ==channel ){
                        log.info("用户不在线");
                    ctx.channel().writeAndFlush(Result.fail("id为:"+chatMessage.getToId().toString()+"发送消息失败"+chatMessage.getToId()+"未在线"));
                }else {
                    //私聊
                    Message<Object> message = new Message<>();
                    message.setFromId(chatMessage.getFromId());     //发言者id
                    String a= UUID.randomUUID().toString();
                    log.info("生成的信息唯一id:   "+a);
                    message.setMsgId(a);     //信息唯一id
                    message.setNickname(chatMessage.getNickname());     //发言者
                    message.setAddTime(DateUtil.date().toLocalDateTime());//当前时间 发送时间
                    message.setMsgcontent(chatMessage.getMsgcontent());     //发送内容
                    message.setToId(chatMessage.getToId());                            //发给谁
                    message.setHeadImg(chatMessage.getHeadImg());  //发送者头像
                    message.setMsgType(1);                         //代表私聊
                    ImServer.GROUP.writeAndFlush(Result.success(message));
                    log.info("私聊:"+chatMessage.getNickname()+"发送:"+chatMessage.getMsgcontent());
                    channel.writeAndFlush(Result.success(message));
                }
            }
            //群发
            //Type 填2   私聊

            case GROUP -> {
                Message<Object> message = new Message<>();
                message.setFromId(chatMessage.getFromId());     //发言者id
                message.setMsgId(UUID.randomUUID().toString());
                message.setNickname(chatMessage.getNickname());     //发言者
                message.setAddTime(DateUtil.date().toLocalDateTime());//当前时间
                message.setMsgcontent(chatMessage.getMsgcontent());     //发送内容
                message.setToId(0L);                            //0L代表发给所有人
                message.setHeadImg(chatMessage.getHeadImg());  //发送者头像
                message.setMsgType(2);                         //代表群聊
                ImServer.GROUP.writeAndFlush(Result.success(message));
                log.info("群聊:"+chatMessage.getNickname()+":"+chatMessage.getMsgcontent());
            }
            default -> ctx.channel().writeAndFlush(Result.fail("不支持的消息类型"));
        }
    } catch (Exception e) {
        ctx.writeAndFlush(Result.fail("发送消息失败"));
        throw new RuntimeException(e);
    }
}
}
